package com.senno.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.senno.entity.UserInfo;
import com.senno.service.UserInfoService;
import com.senno.service.impl.UserInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

//@Controller 代表当前类是控制层
//@RequestMapping("/user") 起映射作用  类似@WebServlet
@Controller
@RequestMapping("/user")
public class UserController {
    //@Autowired 自动装填   装填对象  用于创建对象实例
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/listPage")
    public String listPage(Model model, int pageNo) {
        Page<UserInfo> page = userInfoService.getUserList(pageNo);
        model.addAttribute("page", page);
        return "pages/userList";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {

        return "pages/userAdd";
    }

    @RequestMapping("/add")
    public String add(Model model,UserInfo user) {
        String id = UUID.randomUUID().toString();
        user.setId(id);

        userInfoService.addUser(user);
        return listPage(model,1);
    }

    //删除
    ///user/${userInfo.id}/deleteById.do
    @RequestMapping("/{id}/deleteById")
    public String deleteById(Model model,@PathVariable("id") String id){
        userInfoService.deleteById(id);
        return listPage(model,1);
    }

}